<template>
  <div>
    <FilterTable ref="table" height="auto" :columns="columns" :isSelectColumn="false" :dataSource="tableList" :onSyncTableData="syncTableFun" :onColumnsChange="columns => (this.columns = columns)">
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="text" style="font-size:14px; font-weight:bold;margin-right:20px;">
          <span v-text="`本月：${rowData.currMonth}`"></span>
          <span v-text="`本月截止提报时间: ${rowData.beginDate}至${rowData.endDate}`"></span>
        </el-button>
        <multiuse-button size="small" type="primary" :click="addHandle">新 建</multiuse-button>
      </template>
    </FilterTable>

    <BaseDialog :visible.sync="isShow" title="新增" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <addCar @close="closeHandler" />
    </BaseDialog>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer">关 闭</el-button>
      <multiuse-button :click="saveByIdsFun" :disabled="isDisabled" type="primary">保 存</multiuse-button>
    </div>
  </div>
</template>
<script>
import moment from 'moment';
import { saveByIds } from '@/api/dealerSale/salesPlan/PlanReportN2';

import addCar from './addCar';

export default {
  props: ['rowData'],
  data() {
    return {
      columns: this.createdTable(),
      tableList: [],
      saveData: [],
      isShow: false,
      isDisabled: true
    };
  },
  components: {
    addCar
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    syncTableFun(data) {
      if (data.length > 0) {
        data.map(val => {
          val.total = parseInt(val.quantity1) + parseInt(val.quantity2 || 0) + parseInt(val.quantity3 || 0);
          this.columns.map(x=>{
            if(x.dataIndex == 'n2'){
              x.children[0].editable = val.isAddTwo == 10041001?true:false;
              x.children[0].defaultEditable = val.isAddTwo == 10041001?true:false;
              x.children[0].editRequired = val.isAddTwo == 10041001?true:false;
            }
            if(x.dataIndex == 'n3'){
              x.children[0].editable = val.isAddThree == 10041001?true:false;
              x.children[0].defaultEditable = val.isAddThree == 10041001?true:false;
              x.children[0].editRequired = val.isAddThree == 10041001?true:false;
            }
          })
      });
          console.log(data);
       this.saveData = data;

      }
    },
    addHandle() {
      this.isShow = true;
    },
    closeHandler(type, data) {
      this.isShow = type;
      if (data) {
        this.isDisabled = false;
        this.tableList = data;
        return;
      }
      this.isDisabled = true;
    },
    closeDrawer() {
      this.$emit('close', false);
    },
    async saveByIdsFun() {
      if (this.BaseTable.GET_REQUIRED_ERROR() != null) return;

      let params = [];
      this.saveData.map(x => {
        let json = {
          quantity2: x.quantity2,
          quantity3: x.quantity3,
          optionCode: x.optionCode
        };
        params.push(json);
      });
      const res = await saveByIds(params);
      if (res.resultCode == 200) {
        this.$notify.success({ title: '提示信息', message: '保存成功' });
        this.$emit('close', false, 'refresh');
      }
    },

    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },

        // {
        //   title: '产品代码',
        //   dataIndex: 'productCode',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '产品名称',
        //   dataIndex: 'productName',
        //   align: 'center',
        //   width: 400,
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: '品牌名称',
          dataIndex: 'brandName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置名称',
          dataIndex: 'configName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装名称',
          dataIndex: 'optionName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装代码',
          dataIndex: 'optionCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        // {
        //   title: '内饰',
        //   dataIndex: 'trimColorName',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '颜色',
        //   dataIndex: 'colorName',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '市场指导价',
        //   dataIndex: 'oemDirectivePrice',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: `${moment()
            .subtract(-1, 'months')
            .format('YYYY年MM月')}(N+1)`,
          dataIndex: 'n1',
          filterType: 'input',
          align: 'center',
          children: [
            {
              title: '提报数量',
              dataIndex: 'quantity1',
              sorter: true,
              filter: true,
              filterType: 'input',
              summation: true
            }
          ]
        },
        {
          title: `${moment()
            .subtract(-2, 'months')
            .format('YYYY年MM月')}(N+2)`,
          dataIndex: 'n2',
          filterType: 'input',
          align: 'center',
          children: [
            {
              title: '提报数量',
              dataIndex: 'quantity2',
              sorter: true,
              filter: true,
              filterType: 'input',
              editRequired: true,
              editable: true,
              defaultEditable: true,
              editType: 'number',
              max: 999999999,
              min: 0,
              summation: true
            }
          ]
        },
        {
          title: `${moment()
            .subtract(-3, 'months')
            .format('YYYY年MM月')}(N+3)`,
          dataIndex: 'n3',
          filterType: 'input',
          align: 'center',
          children: [
            {
              title: '提报数量',
              dataIndex: 'quantity3',
              sorter: true,
              filter: true,
              filterType: 'input',
              editRequired: true,
              editable: true,
              defaultEditable: true,
              editType: 'number',
              max: 999999999,
              min: 0,
              summation: true
            }
          ]
        },
        {
          title: '合计',
          dataIndex: 'total',
          sorter: true,
          filter: true,
          filterType: 'input',
          summation: true
        }
      ];
    }
  }
};
</script>
